package com.qf.web.sys_menu.entity;


import ch.qos.logback.core.joran.util.beans.BeanUtil;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author xufuhan
 * @Date 2024/9/10 20:45
 */

public class MenuTree {

    //生成菜单树的工具类
    public  static List<Menu> makeManuTree(List<Menu> menuList, Long pid) {
        //定义容器
        ArrayList<Menu> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream().filter(item -> item != null && item.getParentId() == pid)
                .forEach(dom -> {
                    Menu menu = new Menu();
                    BeanUtils.copyProperties(dom,menu);
                    //递归查询下级菜单
                    List<Menu> children = makeManuTree(menuList, dom.getMenuId());
                    menu.setChildren(children);
                    list.add(menu);
                });
        return list;
    }
}
